package com.qfedu.mtl.service.impl;

import com.qfedu.mtl.dao.BasicDao;
import com.qfedu.mtl.dao.impl.BasicDaoImpl;
import com.qfedu.mtl.dto.BasicDto;
import com.qfedu.mtl.entity.BasicInfo;
import com.qfedu.mtl.entity.InfoDetail;
import com.qfedu.mtl.query.BasicQuery;
import com.qfedu.mtl.query.SelectQuery;
import com.qfedu.mtl.service.BasiService;
import com.qfedu.mtl.util.page.Pager;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class BasiServiceImpl implements BasiService {
    //引入持久层
    private BasicDao dao = new BasicDaoImpl();

    //添加评估类目
    @Override
    public int add(BasicDto dto) {
        int row = dao.add(dto);
        return row;
    }

    //删除评估类目
    @Override
    public int delete(int id) {
        int row = dao.delete(id);
        return row;
    }
    //批量删除 ，类目
    @Override
    public int deleteBatch(List<Integer> idsss) {
        int i = dao.deleteBatch(idsss);
        return i;
    }



    //查找单条记录，通过id
    @Override
    public BasicInfo select(Integer id) {
        BasicInfo basicInfo = dao.select(id);
        return basicInfo;
    }

    @Override
    public int update(BasicDto dto) {
        int row = dao.update(dto);
        return row;
    }


    //根据类目的id找对应选项
    @Override
    public List<InfoDetail> findDetail(Integer id) {
        List<InfoDetail> infoDetail = dao.findDetail(id);
        return infoDetail;
    }

    //查找所有的类目
    @Override
    public List<BasicInfo> selectAll() {
        List<BasicInfo> basicInfos = dao.selectAll();
        return basicInfos;
    }


    //添加某个类目的选项，提供类目，添加选项
    @Override
    public int addDetail(InfoDetail infoDetail) {
        int row = dao.addDetail(infoDetail);
        return row;
    }

    //删除评估选项 ,根据评估选项表的id
    @Override
    public int deleteDetail(Integer id) {
        int row = dao.deleteDetail(id);
        return row;
    }
    //批量删除选项
    @Override
    public int deleteBatch2(List<Integer> idsss) {
        int i = dao.deleteBatch2(idsss);
        return i;
    }

    @Override
    public int updateDetail(InfoDetail infoDetail) {
        int row = dao.updateDetail(infoDetail);
        return row;
    }

    //根据传入的类目id和选择的选项，来计算总的价格
    @Override
    public int getTotal(int g_id, String ids) {
        int total=0;
        //根据id查对应商品的基本价格
        int basicPrice = dao.findBasicPrice(g_id);
        //查询总的加减的值
        //分割字符串
        String[] idss = ids.split(",");
        //转整型数组
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        for (int i = 0; i < idsss.size(); i++) {
            //查一个选项的加减
            int price = dao.getPrice(g_id,idsss.get(i));
            total += price;
        }
        //price:经过评估后的价格
        int price = basicPrice-total;
        System.out.println("经过评估现在还价值：" + price);
        return price;
    }

    //下面两个方法使用了dao中的四个方法完成两个菜单的分页
    @Override
    public Pager<BasicInfo> findPage(Integer page, Integer size, BasicQuery query) {
        List<BasicInfo> list = dao.selectData(page, size, query);
        Integer total = dao.selectTotal(query);
        Pager<BasicInfo> pager = new Pager<>(list, page, size,total);
        return pager;
    }
    @Override
    public Pager<InfoDetail> findPage2(Integer page, Integer size, SelectQuery query) {
        List<InfoDetail> list = dao.selectData2(page, size, query);
        Integer total = dao.selectTotal2(query);
        Pager<InfoDetail> pager = new Pager<>(list, page, size,total);
        return pager;
    }

    @Override
    public Pager<InfoDetail> findPage3(int id, Integer page, Integer size, SelectQuery query) {
        List<InfoDetail> list = dao.selectData3(id,page, size, query);
        Integer total = dao.selectTotal3(id,query);
        Pager<InfoDetail> pager = new Pager<>(list, page, size,total);
        return pager;
    }


}
